$(function(){
    dateStart();
});

function dateStart(){
    monthArr = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    //年份下拉框实现
    var year = new Date().getFullYear();
    for(var i = (year - 30); i < (year + 30);i++){
        document.date.year.options.add(new Option(" " + i + "年", i));
    }
    //月份下拉框
    for (var j = 1; j < 13; j++){ 
        document.date.month.options.add(new Option(" " + j + "月", j));
    }
    document.date.year.value = year;
    document.date.month.value = new Date().getMonth() + 1;
    var y;
    var month = monthArr[new Date().getMonth()];
    if (new Date().getMonth() == 1 && isPingYear(y)) month++;
    writeDay(month);
    document.date.day.value = new Date().getDate();
}
function writeDay(n){
    var d = document.date.day;
    optionsClear(d);
    for (let i = 1; i < (n + 1); i++) {
        d.options.add(new Option(" " + i + "日", i));       
    }
}
function isPingYear(year){
    return(0 == year % 4  && (year %100 != 0 || year %400 == 0));
}
function optionsClear(d){
    d.options.length = 1;
}

function yearDate(str){
    var mon = document.date.month.options[document.date.month.selectedIndex].value;
    if(mon == ""){
        var e = document.date.day;
        optionsClear(e);
        return;
    }
    var n = monthArr[mon-1];
    if(mon == 2 && isPingYear(str)){
        n++;
    }
    writeDay(n);
}
function monthDate(str){
    y = document.date.year.options[document.date.year.selectedIndex].value;
    if(y == ""){
        var e = document.date.day;
        optionsClear(e);
        return; 
    }
    var n = monthArr[str-1];
    if(str == 2 && isPingYear(y)){
        n++;
    }
    writeDay(n);
}
